package com.nbcio.demo.service.impl;

import com.nbcio.common.core.utils.MapstructUtils;
import com.nbcio.common.core.utils.StringUtils;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.nbcio.demo.domain.TestOrderGoods;
import java.util.ArrayList;
import java.util.List;
import com.nbcio.demo.domain.bo.TestOrderMainBo;
import com.nbcio.demo.domain.vo.TestOrderMainVo;
import com.nbcio.demo.domain.TestOrderMain;
import com.nbcio.demo.mapper.TestOrderMainMapper;
import com.nbcio.demo.mapper.TestOrderGoodsMapper;
import com.nbcio.demo.service.ITestOrderMainService;

import java.util.Map;
import java.util.Collection;
import java.util.Collections;

/**
 * 订单主表Service业务层处理
 *
 * @author nbacheng
 * @date 2025-04-24
 */
@RequiredArgsConstructor
@Service
public class TestOrderMainServiceImpl implements ITestOrderMainService {

    private final TestOrderMainMapper baseMapper;
    private final TestOrderGoodsMapper testOrderGoodsMapper;

    /**
     * 查询订单主表
     */
    @Override
    public TestOrderMain queryById(Long id) {
        TestOrderMain testOrderMain = baseMapper.selectById(id);
            List<TestOrderGoods> testOrderGoodsList = testOrderGoodsMapper.selectByMainId(id);
            testOrderMain.setTestOrderGoodsList(testOrderGoodsList);
        return testOrderMain;
    }

        /**
         * 查询订单主表列表
         */
        @Override
        public TableDataInfo<TestOrderMainVo> queryPageList(TestOrderMainBo bo, PageQuery pageQuery) {
            LambdaQueryWrapper<TestOrderMain> lqw = buildQueryWrapper(bo);
            Page<TestOrderMainVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
            return TableDataInfo.build(result);
        }

    /**
     * 查询订单主表列表
     */
    @Override
    public List<TestOrderMainVo> queryList(TestOrderMainBo bo) {
        LambdaQueryWrapper<TestOrderMain> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TestOrderMain> buildQueryWrapper(TestOrderMainBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TestOrderMain> lqw = Wrappers.lambdaQuery();
                    lqw.eq(StringUtils.isNotBlank(bo.getOrderCode()), TestOrderMain::getOrderCode, bo.getOrderCode());
                    lqw.eq(bo.getCustomerId() != null, TestOrderMain::getCustomerId, bo.getCustomerId());
                    lqw.eq(bo.getXdDate() != null, TestOrderMain::getXdDate, bo.getXdDate());
                    lqw.eq(bo.getMoney() != null, TestOrderMain::getMoney, bo.getMoney());
        return lqw;
    }

    /**
     * 新增订单主表
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean insertByBo(TestOrderMainBo bo) {
        TestOrderMain add = MapstructUtils.convert(bo, TestOrderMain.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            // 插入子表数据
            insertTestOrderGoods(add);
        }
        return flag;
    }

    /**
 * 修改订单主表
 */
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean updateByBo(TestOrderMainBo bo) {
    TestOrderMain update = MapstructUtils.convert(bo, TestOrderMain.class);
    validEntityBeforeSave(update);

    // 更新主表
    boolean flag = baseMapper.updateById(update) > 0;

    // 更新子表
    if (flag) {
        // 获取子表数据
        Long id = update.getId();
        List<TestOrderGoods> testOrderGoodsList = update.getTestOrderGoodsList();
        testOrderGoodsMapper.deleteTestOrderGoodsByMainId(id);

        // 插入新的子表数据
        if (StringUtils.isNotNull(testOrderGoodsList)) {
            for (TestOrderGoods testOrderGoods : testOrderGoodsList) {
                testOrderGoods.setMainId(id);
            }
            testOrderGoodsMapper.insertBatch(testOrderGoodsList);
        }
    }

    return flag;
}

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TestOrderMain entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单主表
     */
    @Transactional
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        testOrderGoodsMapper.deleteTestOrderGoodsByMainIds(ids);
        return baseMapper.deleteByIds(ids) > 0;
    }


        /**
         * 新增订单子表信息
         *
         * @param testOrderMain 订单主表对象
         */
        public void insertTestOrderGoods(TestOrderMain testOrderMain) {
            List<TestOrderGoods> testOrderGoodsList = testOrderMain.getTestOrderGoodsList();
            Long id = testOrderMain.getId();
            if (StringUtils.isNotNull(testOrderGoodsList)) {
                List<TestOrderGoods> list = new ArrayList<>();
                for (TestOrderGoods testOrderGoods : testOrderGoodsList) {
                    testOrderGoods.setMainId(id);
                    list.add(testOrderGoods);
                }
                if (list.size() > 0) {
                    testOrderGoodsMapper.insertBatch(list);
                }
            }
        }

        /**
         * 查询订单子表列表
         *
         * @param testOrderMain 订单主表对象
         */
        public List<TestOrderGoods> queryTestOrderGoodsList(TestOrderMain testOrderMain) {
            Long id = testOrderMain.getId();
            return testOrderGoodsMapper.selectList(Wrappers.lambdaQuery(TestOrderGoods.class)
                    .eq(TestOrderGoods::getMainId, id));
        }

}
